package com.ibeacon.controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ibeacon.controller.common.PageBase;
import com.ibeacon.model.UserInfo;
import com.ibeacon.model.WeChatUserInfo;
import com.ibeacon.service.LoginService;
import com.ibeacon.service.WeixinApiService;

@Controller
@Scope("prototype")
public class WeixinApiController extends PageBase {

	@Autowired
	private WeixinApiService weixinApiService;
	@Autowired
	private LoginService loginService;

	@InitBinder("userInfo")
	public void initBinder(WebDataBinder binder) {
		binder.setFieldDefaultPrefix("userInfo.");
	}

	@InitBinder("siteInfo")
	public void initBinderSite(WebDataBinder binder) {
		binder.setFieldDefaultPrefix("siteInfo.");
	}

	// 起始页面
	@RequestMapping("/login")
	public String login(ModelMap model, String code, String id,
			HttpServletResponse response, HttpServletRequest request)
			throws Exception {
		if (id != null) {
			try {
				setCookie("bigdata_id", id + "", response);
			} catch (Exception ex) {
				System.out.println("id加cookie有问题");
			}
		}
		System.out.println("********当前位置:controller**********");
		System.out.println("获取到的code值为:" + code);
		System.out.println("获取到的id为：" + id);
		System.out.println("***************end*******************");
		WeChatUserInfo weChatUserInfo = weixinApiService.getUserInfo(code, id,
				response);
		if (weChatUserInfo != null && weChatUserInfo.getUrl() != null
				&& !"".equals(weChatUserInfo.getUrl())) {
			response.sendRedirect(weChatUserInfo.getUrl());
			return null;
		}
		try {
			setCookie("bigdata_openid", weChatUserInfo.getOpenid(), response);
			setCookie("bigdata_nickname", weChatUserInfo.getNickname(),
					response);
			setCookie("bigdata_headimgurl", weChatUserInfo.getHeadimgurl(),
					response);
		} catch (Exception e) {
			System.out.println("存cookie有问题");
		}
		model.addAttribute("info", weChatUserInfo);
		try {
			model.addAttribute("id", getCookie("bigdata_id", request));
		} catch (Exception e) {
			model.addAttribute("id", "1");
		}
		return "signin";
	}

	// 登录前判断
	@RequestMapping("/foreLogin")
	@ResponseBody
	public String foreLogin(HttpServletResponse response,
			HttpServletRequest request) throws Exception {
		String openid = getCookie("bigdata_openid", request);
		if (openid != null) {
			UserInfo userInfo = loginService.getUserInfo(openid);
			if (userInfo != null) {
				try {
					setCookie("bigdata_userid", userInfo.getUserId(), response);
					setCookie("wx_type", userInfo.getWxType() + "", response);
					setCookie("bigdata_rolename", userInfo.getRoleName(),
							response);
					setCookie("bigdata_username", userInfo.getUserName(),
							response);
				} catch (Exception e) {
					System.out.println("错误:" + e.getMessage());
				}
				return "1";
			}
		}
		return "0";
	}

	// 登录
	@RequestMapping("/loginwx")
	public String loginwx(ModelMap model, String id, UserInfo userInfo,
			HttpServletResponse response, HttpServletRequest request)
			throws Exception {
		String openid = getCookie("bigdata_openid", request);
		userInfo.setOpenid(openid);
		String msg = loginService.login(userInfo);
		if (msg == "") {
			UserInfo userInfo2 = loginService.getUserInfo(openid);
			if (userInfo != null) {
				try {
					setCookie("bigdata_userid", userInfo2.getUserId(), response);
					setCookie("wx_type", userInfo2.getWxType() + "", response);
					setCookie("bigdata_rolename", userInfo2.getRoleName(),
							response);
					setCookie("bigdata_username", userInfo2.getUserName(),
							response);
				} catch (Exception e) {

				}
			}
			if (id == "2") {
				return "redirect:/index";
			} else {
				return "user";
			}
		} else {
			model.addAttribute("id", id);
			model.addAttribute("userInfo", userInfo);
			model.addAttribute("message", msg);
			return "signin";
		}
	}

	@RequestMapping("/signin")
	public String signin(HttpServletResponse response,
			HttpServletRequest request) throws Exception {
		DeleteCookie("bigdata_userid", response);
		DeleteCookie("bigdata_username", response);
		DeleteCookie("bigdata_rolename", response);
		return "signin";
	}

}
